package com.androidx.p013lv.base.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/* renamed from: com.androidx.lv.base.utils.ActivityUtil */
public class ActivityUtil {
    private static final String TAG = "ActivityUtil";
    private static ActivityUtil sActivityUtil;
    public Stack<Activity> mActivityStack = new Stack<>();

    private ActivityUtil() {
    }

    public static ActivityUtil getInstance() {
        if (sActivityUtil == null) {
            synchronized (ActivityUtil.class) {
                if (sActivityUtil == null) {
                    sActivityUtil = new ActivityUtil();
                }
            }
        }
        return sActivityUtil;
    }

    public void addActivity(Activity activity) {
        this.mActivityStack.add(activity);
    }

    public void removeActivity(Activity activity) {
        if (activity != null && this.mActivityStack.contains(activity)) {
            this.mActivityStack.remove(activity);
        }
    }

    public Activity getCurrentActivity() {
        return this.mActivityStack.lastElement();
    }

    public Activity getActivity(Class<?> cls) {
        Stack<Activity> stack = this.mActivityStack;
        if (stack == null) {
            return null;
        }
        Iterator<Activity> it = stack.iterator();
        while (it.hasNext()) {
            Activity next = it.next();
            if (next.getClass().equals(cls)) {
                return next;
            }
        }
        return null;
    }

    public void finishCurrentActivity() {
        finishActivity(this.mActivityStack.lastElement());
    }

    public void finishActivity(Activity activity) {
        if (activity != null && this.mActivityStack.contains(activity)) {
            this.mActivityStack.remove(activity);
            activity.finish();
        }
    }

    public void finishActivity(Class<?> cls) {
        Iterator<Activity> it = this.mActivityStack.iterator();
        while (it.hasNext()) {
            Activity next = it.next();
            if (next.getClass().equals(cls)) {
                finishActivity(next);
                return;
            }
        }
    }

    public void finishAllActivity() {
        for (int size = this.mActivityStack.size() - 1; size >= 0; size--) {
            if (this.mActivityStack.get(size) != null) {
                finishActivity(this.mActivityStack.get(size));
            }
        }
        this.mActivityStack.clear();
    }

    public void finishAllActivityExcept(Class<?> cls) {
        for (int size = this.mActivityStack.size() - 1; size >= 0; size--) {
            if (this.mActivityStack.get(size) != null && !this.mActivityStack.get(size).getClass().equals(cls)) {
                finishActivity(this.mActivityStack.get(size));
            }
        }
    }

    public void exitApp() {
        try {
            finishAllActivity();
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable th) {
            System.exit(0);
            throw th;
        }
        System.exit(0);
    }

    public static boolean isForeground(Context context, String str) {
        List<ActivityManager.RunningTaskInfo> runningTasks;
        if (context == null || TextUtils.isEmpty(str) || (runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1)) == null || runningTasks.size() <= 0 || !str.equals(runningTasks.get(0).topActivity.getClassName())) {
            return false;
        }
        return true;
    }
}
